package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.VendorBean;
import com.project.mapper.IVendorMapper;
import com.project.service.IVendorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class VendorServiceImpl implements IVendorService {
    @Autowired
    private IVendorMapper vendorMapper;
    @Override
    public IPage<VendorBean> findByItem(Integer pageNO, String name, String ranges) {
        QueryWrapper qw=new QueryWrapper<>();
        if (name!=null && !name.equals("")){
            qw.like("v_name",name);
        }
        if (ranges!=null && !ranges.equals("")){
            qw.like("v_ranges",ranges);
        }
        return vendorMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public VendorBean findById(Integer vendorId) {
        return vendorMapper.selectById(vendorId);
    }

    @Override
    public void updateVendor(Integer vendorId, String headerName, String headerPhone) {
        VendorBean vendor=vendorMapper.selectById(vendorId);
        vendor.setHeaderName(headerName);
        vendor.setHeaderPhone(headerPhone);
        vendorMapper.updateById(vendor);
    }

    @Override
    public void addVendor(VendorBean vendor) {
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("v_name",vendor.getName());
        VendorBean vendorBean=vendorMapper.selectOne(qw);
        //对象为空，说明没有该供应商，执行添加
        if (vendorBean==null){
            vendorMapper.insert(vendor);
        }
    }

    @Override
    public List<VendorBean> findAll() {
        return vendorMapper.selectList(null);
    }
}
